//
//  OrderCell.swift
//  BaseSwift
//
//  Created by gh on 2019/10/4.
//  Copyright © 2019 ghwang. All rights reserved.
//

import UIKit

class OrderCell: UITableViewCell {

    var curOrder:OrderVo?
    let containerView = UIView()
    let noLab = UILabel()
    let statusLab = UILabel()
    let photo = UIImageView()
    let nameLab = UILabel()
    let numLab = UILabel()
    let priceLab = UILabel()
    let cancelBtn = UIButton()
    let rightBtn = UIButton()
    var rightClick:((OrderVo,UIButton) -> Void)?
    var cancelClick:((OrderVo) -> Void)?

    

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.contentView.addSubview(containerView)
        self.containerView.sd_cornerRadius = 8
        self.backgroundColor = UIColor.backgroundColor
        containerView.backgroundColor = UIColor.white
        self.selectionStyle = .none

        let _ = containerView.sd_layout()?.spaceToSuperView(UIEdgeInsets(top: 12, left: 12, bottom: 0, right: 12))

        // shadowCode
        containerView.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
        containerView.layer.shadowOffset = CGSize(width: 0, height: 0)
        containerView.layer.shadowOpacity = 1
        containerView.layer.shadowRadius = 20
        
        containerView.sd_addSubviews([noLab,statusLab,photo,nameLab,numLab,priceLab])
        
        noLab.font = UIFont.smallerFont
        noLab.textColor = UIColor.textPlaceHolderColor
        noLab.text = "订单号:00000000000"
        let _ = noLab.sd_layout()?.topSpaceToView(containerView,18)!.heightIs(18)!.leftSpaceToView(containerView,16)!.widthIs(225)
        
        statusLab.font = UIFont.commonFontSmaller
        statusLab.textColor = UIColor.btnMainColor
        statusLab.text = "待付款"
        let _ = statusLab.sd_layout()?.topSpaceToView(containerView,18)!.heightIs(18)!.rightSpaceToView(containerView,16)!
        statusLab.setSingleLineAutoResizeWithMaxWidth(188)
        
        photo.backgroundColor = UIColor.cccColor
        let _ = photo.sd_layout()?.widthIs(80)!.heightIs(80)!.leftSpaceToView(containerView,16)!.topSpaceToView(noLab,20)
        
        nameLab.font = UIFont.commonFont
        nameLab.textColor = UIColor.textTitleColorBlack
        nameLab.text = "订单名称订单名称订单名称订单名称订单名称"
        let _ =  nameLab.sd_layout()?.leftSpaceToView(photo,10)!.topSpaceToView(noLab,28)!.heightIs(20)!.rightSpaceToView(containerView,16)
        
        numLab.font = UIFont.smallFont
        numLab.textColor = UIColor.textPlaceHolderColor
        numLab.text = "2成人"
        let _ = numLab.sd_layout()?.topSpaceToView(nameLab,20)!.heightIs(15)!.leftSpaceToView(photo,16)!.widthIs(170)
        
        priceLab.font = UIFont.commonFontSmall
        priceLab.textColor = UIColor.textTitleColorBlack
        priceLab.text = "￥19800.00"
        let _ = priceLab.sd_layout()?.topSpaceToView(nameLab,20)!.heightIs(20)!.rightSpaceToView(containerView,16)!
        priceLab.setSingleLineAutoResizeWithMaxWidth(199)
        
        
        containerView.addSubview(rightBtn)
        rightBtn.addTarget(self, action: #selector(rightBtnClick), for: .touchUpInside)
        rightBtn.setTitle("立即支付", for: .normal)
        rightBtn.titleLabel?.font = UIFont.commonFontSmaller
        rightBtn.setTitleColor(UIColor.btnMainColor, for: .normal)
        let _ = rightBtn.sd_layout()?.widthIs(88)!.heightIs(28)!.rightSpaceToView(containerView,16)!.bottomSpaceToView(containerView,16)
        rightBtn.layer.borderColor = UIColor.btnMainColor.cgColor
        rightBtn.layer.borderWidth = 1
        rightBtn.sd_cornerRadius = SMALL_BTN_RADIUS

        
        
        containerView.addSubview(cancelBtn)
        cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: .touchUpInside)

        cancelBtn.titleLabel?.font = UIFont.commonFontSmaller
        cancelBtn.setTitle("取消订单", for: .normal)
        cancelBtn.setTitleColor(UIColor.tabbarDefaultTextColor, for: .normal)
        let _ = cancelBtn.sd_layout()?.widthIs(88)!.heightIs(28)!.rightSpaceToView(rightBtn,16)!.bottomSpaceToView(containerView,16)
        cancelBtn.layer.borderColor = UIColor.textTitleColorBlack.cgColor
        cancelBtn.layer.borderWidth = 1
        cancelBtn.sd_cornerRadius = SMALL_BTN_RADIUS
        
    }

    func setModel(mod:OrderVo){
        
        self.curOrder = mod
        noLab.text = "订单号:\(mod.orderNo!)"

        nameLab.text  = "\(mod.pdtName!)[\(mod.proName!)]"
        numLab.text = "\(mod.amountAdult!)成人 \(mod.amountChildren!)儿童"
        priceLab.text =  "￥\(mod.amountAll!)"
        statusLab.text = Constant.getOrderStatus(status: mod.orderStatus!, pay: mod.payStatus!)
        
        

        
        photo.sd_setImage(with: URL(string:mod.pdtImg), placeholderImage: UIImage(named: "holder"), options: .lowPriority) { (img, B, C, D) in
            
            SDImageCache.shared.store(img, forKey: mod.pdtImg, toDisk: true) {
                
            }
            
        }
        
        if mod.orderStatus == "NEW" {
            
            cancelBtn.isHidden = false
            rightBtn.isHidden = false
            
        }else if mod.orderStatus == "FAILED"{
            
            cancelBtn.isHidden = true
            rightBtn.isHidden = true
            
        }else if mod.orderStatus == "SUCCESS"{
            
            rightBtn.isHidden = false
            cancelBtn.isHidden = true
            rightBtn.setTitle("退款", for: .normal)

        }else if mod.orderStatus == "FINISH"{
            
            if mod.payStatus == "PAYED" {
                
                rightBtn.isHidden = false
                cancelBtn.isHidden = true
                rightBtn.setTitle("评论", for: .normal)
                
            }else{
                
                cancelBtn.isHidden = true
                rightBtn.isHidden = true
            }
        }else if mod.orderStatus == "APPRAISED"{
            
            cancelBtn.isHidden = true
            rightBtn.isHidden = true
            
        }else if mod.orderStatus == "REFUNDING"{
            
            cancelBtn.isHidden = true
            rightBtn.isHidden = true
            
        }
        

    }
    @objc func rightBtnClick(btn:UIButton){
        
        self.rightClick!(self.curOrder!,btn)
    }
    @objc func cancelBtnClick(){
        
        self.cancelClick!(self.curOrder!)
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
